package cn.iocoder.yudao.module.blog.service.caselike;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.blog.dal.dataobject.follow.BlogFollowDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.caselike.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.caselike.BlogCaseLikeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.caselike.BlogCaseLikeConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.caselike.BlogCaseLikeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 案例点赞 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogCaseLikeServiceImpl implements BlogCaseLikeService {

    @Resource
    private BlogCaseLikeMapper caseLikeMapper;

    @Override
    public Long createCaseLike(BlogCaseLikeCreateReqVO createReqVO) {
        // 插入
        BlogCaseLikeDO caseLike = BlogCaseLikeConvert.INSTANCE.convert(createReqVO);
        caseLikeMapper.insert(caseLike);
        // 返回
        return caseLike.getId();
    }

    @Override
    public void updateCaseLike(BlogCaseLikeUpdateReqVO updateReqVO) {
        // 校验存在
        validateCaseLikeExists(updateReqVO.getId());
        // 更新
        BlogCaseLikeDO updateObj = BlogCaseLikeConvert.INSTANCE.convert(updateReqVO);
        caseLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteCaseLike(Long id) {
        // 校验存在
        validateCaseLikeExists(id);
        // 删除
        caseLikeMapper.deleteById(id);
    }

    private void validateCaseLikeExists(Long id) {
        if (caseLikeMapper.selectById(id) == null) {
            throw exception(CASE_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public BlogCaseLikeDO getCaseLike(Long id) {
        return caseLikeMapper.selectById(id);
    }

    @Override
    public List<BlogCaseLikeDO> getCaseLikeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return caseLikeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogCaseLikeDO> getCaseLikePage(BlogCaseLikePageReqVO pageReqVO) {
        return caseLikeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogCaseLikeDO> getCaseLikeList(BlogCaseLikeExportReqVO exportReqVO) {
        return caseLikeMapper.selectList(exportReqVO);
    }

    @Override
    public List<BlogCaseLikeDO> getCaseLikeList(Long memberId, Collection<Long> caseIds){
        LambdaQueryWrapperX<BlogCaseLikeDO> wrapperX = new LambdaQueryWrapperX<BlogCaseLikeDO>();
        wrapperX.eq(BlogCaseLikeDO::getMemberId, memberId).in(BlogCaseLikeDO::getCaseId, caseIds);

        List<BlogCaseLikeDO> list = caseLikeMapper.selectList(wrapperX);

        return list;
    }

}
